package com.semidata.trp.business.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.semidata.rtd.core.http.server.AbstractDispatchHandler;
import com.semidata.rtd.core.http.server.HttpSession;
import com.semidata.rtd.core.json.JsonArray;
import com.semidata.rtd.core.json.JsonObject;
import com.semidata.rtd.core.log.Logger;
import com.semidata.rtd.core.service.ServiceManager;
import com.semidata.trp.business.report.MonitorReportData;
import com.semidata.trp.business.report.ReportService;
import com.semidata.trp.business.utils.ReportUtils;

public class ReportProviderHttpHandler extends AbstractDispatchHandler {

	public ReportProviderHttpHandler(ChannelHandlerContext ctx,
			HttpRequest request) {
		super(ctx, request);
	}

	@Override
	public void handle(HttpSession session) {

		try {
			ReportService service = ServiceManager
					.getService(ReportService.class);
			Set<MonitorReportData> base = service.getBaseData()
					.getMonitorReportData();
			Set<MonitorReportData> current = service.getCurrentData()
					.getMonitorReportData();
			JsonObject ret = new JsonObject();
			Logger.log("INFO", "a request from web server for report data");
			JsonObject obj = new JsonObject(session.getBody());

			JsonArray orgCosts = obj.getArray("organizationCosts");
			if (orgCosts != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < orgCosts.size(); i++) {
					ids.add((Integer) orgCosts.get(i));
				}

				ret.putArray("organizationCosts",
						ReportUtils.getOrganizationCosts(base, current, ids));
			}

			JsonArray resourceImpressions = obj.getArray("resourceImpressions");
			if (resourceImpressions != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < resourceImpressions.size(); i++) {
					ids.add((Integer) resourceImpressions.get(i));
				}

				ret.putArray("resourceImpressions",
						ReportUtils.getResourceImpressions(base, current, ids));
			}

			JsonArray resourceClicks = obj.getArray("resourceClicks");
			if (resourceClicks != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < resourceClicks.size(); i++) {
					ids.add((Integer) resourceClicks.get(i));
				}

				ret.putArray("resourceClicks",
						ReportUtils.getResourceClicks(base, current, ids));
			}

			JsonArray strategyCosts = obj.getArray("strategyCosts");
			if (strategyCosts != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < strategyCosts.size(); i++) {
					ids.add((Integer) strategyCosts.get(i));
				}

				ret.putArray("strategyCosts",
						ReportUtils.getStrategyCosts(base, current, ids));
			}

			JsonArray strategyImpressions = obj.getArray("strategyImpressions");
			if (strategyImpressions != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < strategyImpressions.size(); i++) {
					ids.add((Integer) strategyImpressions.get(i));
				}

				ret.putArray("strategyImpressions",
						ReportUtils.getStrategyImpressions(base, current, ids));
			}

			JsonArray strategyClicks = obj.getArray("strategyClicks");
			if (strategyClicks != null) {
				List<Integer> ids = new ArrayList<>();
				for (int i = 0; i < strategyClicks.size(); i++) {
					ids.add((Integer) strategyClicks.get(i));
				}

				ret.putArray("strategyClicks",
						ReportUtils.getStrategyClicks(base, current, ids));
			}
			Logger.log("INFO", "web get data : " + ret.toString());

			session.getResponse().end(ret.toString());
		} catch (Exception e) {
			e.printStackTrace();
			session.getResponse().error();
		}
	}
}
